package com.sdgakj.repository;

import com.sdgakj.entity.FeedComment;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.List;

public class FeedCommentDaoImpl {

	@PersistenceContext
	private EntityManager em;

	/**
	 * 时间倒叙帖子列表
	 * 
	 * @param feedId
	 * @param sinceId
	 * @param maxId
	 * @param pageSize
	 * @return
	 */
	public List<FeedComment> findTimeline(Long feedId, Long sinceId, Long maxId, Integer pageSize) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT \n");
		sql.append(" t1.* \n");
		sql.append("FROM \n");
		sql.append("  f_feed_comment t1 \n");
		sql.append("WHERE 1 = 1 \n");
		sql.append("  AND t1.deltag = '0' \n");
		if(feedId != null) {
			sql.append("  AND t1.feed_id = :feedId \n");
		}
		if(sinceId != null) {
			sql.append("  AND t1.id > :sinceId \n");
		} else if(maxId != null) {
			sql.append("  AND t1.id < :maxId \n");
		}
		sql.append("ORDER BY t1.id DESC \n");
		sql.append("LIMIT 0, :rows \n");
		Query q = em.createNativeQuery(sql.toString(), FeedComment.class);
        if(feedId != null) {
        	q.setParameter("feedId", feedId);
		}
		if(sinceId != null) {
			q.setParameter("sinceId", sinceId);
		} else if(maxId != null) {
			q.setParameter("maxId", maxId);
		}
		q.setParameter("rows", pageSize);
//		List<Feed> result = q.getResultList();
		return q.getResultList();
//		sql = new StringBuffer();
//        sql.append("SELECT \n");
//        sql.append("  COUNT(1) \n");
//        sql.append("FROM \n");
//		sql.append("  sht_feed t1 \n");
//		sql.append("  AND t1.deltag = '0' \n");
//		if(customerId != null) {
//			sql.append("  AND t1.customer_id = :customerId \n");
//		}
//		if(tagId != null) {
//			sql.append("  AND t1.tag_id = :tagId \n");
//		}
//		if(sinceId != null) {
//			sql.append("  AND t1.id > :sinceId \n");
//		} else if(maxId != null) {
//			sql.append("  AND t1.id < :maxId \n");
//		}
//        q = em.createNativeQuery(sql.toString());
//        long total = ((BigInteger) q.getSingleResult()).longValue();
//
//        Pageable pageable = new PageRequest(sinceId.intValue() / pageSize, pageSize);
//        List<Feed> result = new PageImpl<Feed>(resutcontent, pageable, total);
//		return result;
	}
	
	/**
	 * 未删除贴子的所有评论
	 * @param feedId 贴子id
	 * @param pageNumber 
	 * @param pageSize
	 * @return
	 */
    public List<FeedComment> findPageComment(Long feedId, Integer pageNumber, Integer pageSize){
    	StringBuffer strSql =new StringBuffer();
    	strSql.append("select c1.* from sht_feed_comment c1 \n");
    	strSql.append(" where 1=1 \n");
		strSql.append("  AND c1.deltag = '0' \n");
		if(feedId != null) {
			strSql.append("  AND c1.feed_id = :feedId \n");
		}
		strSql.append("ORDER BY c1.id asc \n");
		strSql.append("LIMIT :startindex, :rows \n");
        Query q = em.createNativeQuery(strSql.toString(), FeedComment.class);
		 if(feedId != null) {
	        	q.setParameter("feedId", feedId);
		 }
		 q.setParameter("startindex", (pageNumber-1) * pageSize);
		 q.setParameter("rows", pageSize);
		return q.getResultList();
    }

    /**
     * 未删除贴子的评论数
     * @param feedId
     * @return
     */
    public Long getTotalCount(Long feedId){
		StringBuffer sql = new StringBuffer();
		sql = new StringBuffer();
		sql.append("SELECT \n");
		sql.append("  COUNT(1) from sht_feed_comment s \n");
		sql.append("WHERE 1 = 1 \n");
		if(feedId !=null){
			sql.append(" AND s.feed_id = :feedId");
		}
		sql.append(" AND s.deltag = '0'");
		Query q = em.createNativeQuery(sql.toString());
		if(feedId !=null){
			q.setParameter("feedId", feedId);
		}
		long total = ((BigInteger) q.getSingleResult()).longValue();
		return total;	
    }
}
